//
//  ClassScheduleCardViewController.m
//  ai2
//
//  Created by 杜超男 on 2019/3/19.
//  Copyright © 2019年 杜超男. All rights reserved.
//

#import "ClassScheduleCardViewController.h"
#import "ClassScheduleCardTableViewCell.h"
#import "TwoHeaderView.h"
#import "Masonry.h"
@interface ClassScheduleCardViewController ()<UITableViewDelegate , UITableViewDataSource>
{
    NSArray *rowArray;
    NSArray *sectionArray;
    NSMutableDictionary *showDic;//用来判断分组展开与收缩的
    NSString *key;
}
@property (nonatomic , strong)UITableView *tableView;
@property (nonatomic , strong)NSArray *WeekArray;
@end

@implementation ClassScheduleCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    showDic = [[NSMutableDictionary alloc]init];
    self.WeekArray = [[NSArray alloc] initWithObjects:@"星期一" , @"星期二" , @"星期三" , @"星期四" , @"星期五" , @"星期六" , @"星期天", nil];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    self.tableView.rowHeight = 130;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"ClassScheduleCardTableViewCell" bundle:nil] forCellReuseIdentifier:@"ClassScheduleCardTableViewCell"];
    [self.view addSubview:self.tableView];
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_safeAreaLayoutGuideTop).inset(100);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(0);
    }];
    // Do any additional setup after loading the view from its nib.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.WeekArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ClassScheduleCardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ClassScheduleCardTableViewCell"];
    if (!cell) {
        cell = [[ClassScheduleCardTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ClassScheduleCardTableViewCell"];
//        cell.clipsToBounds = YES;
    }
    
    //rgba(103/255.0, 194/255.0, 86/255.0, 1)
//    cell.backgroundColor = [UIColor colorWithRed:103/255.0 green:194/255.0 blue:86/255.0 alpha:1];
    cell.backgroundColor = [UIColor whiteColor];
//    self.tableView.rowHeight = 130;
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.clipsToBounds = YES;
//    cell.textLabel.text = self.WeekArray[indexPath.section];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 点击了才给行高 , 其实都有行内容只不过行高是0
    if ([showDic objectForKey:[NSString stringWithFormat:@"%ld",indexPath.section]]) {
        return 130;
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    TwoHeaderView *header = [[TwoHeaderView alloc] initWithFrame:CGRectMake(-10, 0, self.view.frame.size.width+20, 50)];
    header.groupName = self.WeekArray[section];
    // 图片  (存的 一个样式,不存的一个样式)
    if (![showDic objectForKey:key]) {
        header.open = YES;
    }else{
        header.open = NO;
    }
    // 单击的 Recognizer ,收缩分组cell
    header.tag = section;
    
    UITapGestureRecognizer *singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTap:)];
    
    [header addGestureRecognizer:singleRecognizer];//添加一个手势监测；
    return header;
}
#pragma mark 展开收缩section中cell 手势监听
-(void)SingleTap:(UITapGestureRecognizer*)recognizer{
    // 点哪个  ->n
    NSInteger didSection = recognizer.view.tag;
    
    // 把第n个存起来
    key = [NSString stringWithFormat:@"%ld",didSection];
    if (![showDic objectForKey:key]) {// 没值 就存
        // 目的是存起来 存啥不重要,点谁存谁,  取得时候 谁有值意为着谁被点开了.
        //        headerView.open = YES;
        [showDic setObject:@"有值即可" forKey:key];
        //切换展开和收起状态
        
        
    }else{ // 如果有值 就删 (删除了 上边就取不到值了  行高->0)
        //        headerView.open = NO;
        [showDic removeObjectForKey:key];
        
    }
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:didSection] withRowAnimation:UITableViewRowAnimationFade];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
- (IBAction)BackBtn:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
